import dt from '@dt/dt'
import orderApi from '@/config/server/api/order.js'
import userApi from '@/config/server/api/user.js'
import baseApi from '@/config/server/api/base.js'
import buildingApi from '@/config/server/api/building'

let positionList = [],
  channelList = [],
  productList = []

function detail() {
  return orderApi.bonus.params.detail
    .fetch({
      housesId: dt.buildInfo().housesId
    })
    .then((res) => ({
      ...res,
      leaderDistributionFlag: res.leaderDistributionFlag == '1' ? true : false,
      boDimissionList: [
        {
          key: 'subscription',
          name: '认购未签约',
          departure: res.subscriptionDeparture || 0,
          associate: res.subscriptionAssociate || 0
        },
        {
          key: 'contract',
          name: '签约未全款',
          departure: res.contractDeparture || 0,
          associate: res.contractAssociate || 0
        },
        {
          key: 'payment',
          name: '全款未结佣',
          departure: res.paymentDeparture || 0,
          associate: res.paymentAssociate || 0
        }
      ]
    }))
}

function update(info) {
  info = {
    ...info,
    housesId: dt.buildInfo().housesId,
    leaderDistributionFlag: info.leaderDistributionFlag ? '1' : '2'
  }
  for (let item of info.boDimissionList) {
    info[item.key + 'Departure'] = item.departure
    info[item.key + 'Associate'] = item.associate
  }
  delete info.boDimissionList
  return orderApi.bonus.params.update.fetch(info)
}

// 修改奖金单操作
function validTime(info) {
  info.housesId = dt.buildInfo().housesId
  console.log(info)
  return orderApi.bonus.params.validTime.fetch(info)
}

// 获取参与奖金的岗位列表
function getPosition() {
  return userApi.jobPostManagement.bonusPosition.fetch().then((res) => {
    let data = res.map((i) => ({
      key: i.id,
      value: i.positionName
    }))
    positionList = data
    return data
  })
}

function getChannel() {
  return baseApi.caseChannel.channleList
    .fetch({
      buildingId: dt.buildInfo().housesId
    })
    .then((res) => {
      // 优先展示渠道带看的渠道
      // 渠道类型为"渠道带看"且合作类型为"同策合作"的在渠道名称后显示"渠道带看"
      let takeLook = res.filter((i) => i.isTakeLook == '1'),
        other = res.filter((i) => i.isTakeLook != '1')
      let arr = [...takeLook, ...other]
      arr = arr.map((i) => ({
        key: i.channelId,
        value: i.isTakeLook == '1' && i.cooperationType == '1' ? `${i.channelName} (渠道带看)` : i.channelName
      }))
      channelList = arr
      return arr
    })
}

function getProducts() {
  return buildingApi.product.listByHousesId
    .fetch({
      housesId: dt.buildInfo().housesId
    })
    .then((res) => {
      let data = res.map((i) => ({
        key: i.productId,
        value: i.productName
      }))
      productList = data
      return data
    })
}

function positions(list, row, overlap = true) {
  let data = [...positionList]
  if (overlap) {
    let index = list.findIndex((i) => i == row)
    list.forEach((item, idx) => {
      if (idx !== index) {
        for (let it of item.postIds) {
          data = data.filter((i) => i.key != it)
        }
      }
    })
  }
  return data
}

function products(list, row, overlap = true) {
  let data = [...productList]
  if (overlap) {
    let index = list.findIndex((i) => i == row)
    list.forEach((item, idx) => {
      if (idx !== index) {
        for (let it of item.productIds) {
          data = data.filter((i) => i.key != it)
        }
      }
    })
  }
  return data
}

function channels(list, row) {
  return channelList
}

export default {
  detail,
  update,
  validTime,

  getPosition,
  getChannel,
  getProducts,
  positions,
  products,
  channels
}
